package com.boot.service;

import com.boot.entity.Menu;
import com.boot.entity.User;
import com.boot.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuService {

    @Autowired
    MenuMapper menuMapper;

    public Map<String,Object> page(Integer current,Integer pageSize,String menuName){
        if(current ==null || current < 1){
            current = 1;
        }

        if(pageSize == null || pageSize < 1){
            pageSize = 10;
        }

        Integer start = (current - 1) * pageSize;

        List<Menu> menus = menuMapper.list(start,pageSize,menuName);

        int count = menuMapper.count(menuName);
        int pages = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
        Map<String,Object> map = new HashMap<>();
        map.put("menus",menus);

        map.put("pages",pages);

        return map;
    }

    public Menu getById(Long id) {
        return menuMapper.getById(id);
    }

    public void saveOrUpdate(Menu menu) {
        if(menu.getId()==null){
            //保存
            menuMapper.save(menu);
        }else{
            menuMapper.updateById(menu);
        }
    }

    public void delById(Long id) {
        menuMapper.delById(id);
    }

    public void delByIds(List<Long> ids) {
        menuMapper.delByIds(ids);
    }

    public List<Menu> list() {
        return menuMapper.listAll();
    }
}
